package com.jade.shoesmallinterface.mapper;

import com.jade.shoesmallinterface.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ProductMapper {

    // 查询所有
    @Select("SELECT * FROM product")
    List<Product> selectAllProducts();

    // 查询一个
    @Select("SELECT * FROM product WHERE product_id = #{productId}")
    Product selectProductById(Integer productId);


    // 根据品牌或商品名称查询商品
    @Select({
            "<script>",
            "SELECT * FROM product",
            "WHERE 1=1",
            "<if test='brand != null and brand != \"\"'>",
            "  AND brand = #{brand}",
            "</if>",
            "<if test='productName != null and productName != \"\"'>",
            "  AND product_name LIKE CONCAT('%', #{productName}, '%')",
            "</if>",
            "</script>"
    })
    List<Product> selectProductsByBrandOrName(
            @Param("brand") String brand,
            @Param("productName") String productName
    );
}
